/*
 *  Copyright (C) 2010 Matthias Buch-Kromann <mbk.isv@cbs.dk>
 * 
 *  This file is part of the MatrixParser package.
 *  
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.osdtsystem.matrixparser.parsers;

import java.io.Writer;
import org.osdtsystem.matrixparser.data.CONLLSentence;
import org.osdtsystem.matrixparser.data.CONLLToken;
import org.osdtsystem.matrixparser.features.TwoWayFeatureHandler;
import org.osdtsystem.matrixparser.parsers.output.DependencyTree;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class ActionAddNode extends Action {
    CONLLToken token;

    public ActionAddNode(int node, CONLLSentence sentence) {
        super(0, addNodeAction(node));
        this.token = sentence.get(node);
        if (this.token == null)
            throw new IllegalArgumentException("Node " + node + " at sentence " + sentence + " returned null pointer");
    }

    static int[] addNodeAction(int node) {
        int[] action = { node };
        return action;
    }

    public int node() { return get(0); }

    @Override
    void writeAsDTAG(Writer writer, TwoWayFeatureHandler labels) throws java.io.IOException {
        writer.append("node ").append("" + node())
                .append(" ").append(token.form())
                .append(" lemma=\"").append(token.lemma())
                .append("\" cpostag=\"").append(token.cpostag())
                .append("\" postag=\"").append(token.postag()).append("\"");
        for (String feat : token.feats().split("\\|")) {
            if (feat.contains("=")) {
                String[] pair = feat.split("=");
                writer.append(" ").append(pair[0]).append("=\"").append(pair[1]).append("\"");
            }
        }
        writer.append("\n");
    }
}
